package gestioneDatabase.Post;

import gestioneDatabase.MultipleDatabaseElementUser;

import java.util.ArrayList;

import models.Post;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Modulo atto a recuperare tutti i post del Forum.
 * 
 */
public class AllPostsRetriever extends MultipleDatabaseElementUser {

	private AllPostsRetrieverFiller postFiller = new AllPostsRetrieverFiller();
	private ArrayList<Post> posts = new ArrayList<Post>();

	public AllPostsRetriever() {
		super();
	}

	@Override
	public void execute() {
		posts.clear();
		try {

			NodeList nList = open("DatabasePost", "Post");

			for (int i = 0; i < nList.getLength(); i++) {

				Node nNode = nList.item(i);

				if (nNode.getNodeType() == Node.ELEMENT_NODE) {
					Element eElement = (Element) nNode;

					Post post = postFiller.fill(eElement);
					posts.add(post);
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Restituisce tutti i post del Forum.
	 * 
	 * @return l'arrayList dei post del forum.
	 */

	public ArrayList<Post> getPosts() {
		execute();
		return posts;
	}

}
